package cn.zebra.dev.member.module.controller;

import cn.zebra.dev.common.constant.ParamsConstant;
import cn.zebra.dev.common.restful.Result;
import cn.zebra.dev.ibatis.plus.utils.PageUtils;
import cn.zebra.dev.member.module.address.entity.MemberAddressEntity;
import cn.zebra.dev.member.module.address.service.MemberAddressService;
import cn.zebra.dev.member.module.dto.MemberAddressDTO;
import cn.zebra.dev.member.module.dto.MemberInfoDTO;
import cn.zebra.dev.member.module.login.service.SecurityMemberService;
import cn.zebra.dev.member.module.member.entity.BaseMember;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "用户管理")
@RestController
public class MemberController {
    @Autowired
    private SecurityMemberService securityMemberService;

    @Autowired
    private MemberAddressService memberAddressService;

    @GetMapping("/info")
    @ApiOperation("用户信息")
    public Result<MemberInfoDTO> info() {
        BaseMember member = securityMemberService.getBaseMember();
        MemberInfoDTO dto = new MemberInfoDTO();
        Long memberId = member.getId();
        dto.setId(memberId);
        dto.setMemberName(member.getMemberName());
        dto.setMemberHeadImg(member.getMemberHeadImg());
        dto.setSex(member.getSex());
        dto.setPhone(member.getPhone());
        dto.setEmail(member.getEmail());
        dto.setDay(member.getBirthdayDay());
        dto.setMonth(member.getBirthdayMonth());
        dto.setYear(member.getBirthdayYear());
        return Result.success(dto);
    }

    @GetMapping("/getList")
    @ApiOperation("用户地址列表")
    public Result<PageUtils<MemberAddressDTO>> getList(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        Long memberId = securityMemberService.getMemberId();
        Map<String, Object> params = new HashMap<>();
        params.put(ParamsConstant.PAGE, page);
        params.put("memberId", memberId);
        params.put(ParamsConstant.LIMIT, limit);
        PageUtils<MemberAddressEntity> pagrUtils = memberAddressService.queryPage(params);
        List<MemberAddressEntity> list = pagrUtils.getList();
        List<MemberAddressDTO> list1 = new ArrayList<>();
        list.forEach(entity -> {
            MemberAddressDTO dto = new MemberAddressDTO();
            BeanUtils.copyProperties(entity, dto);
            list1.add(dto);
        });
        PageUtils<MemberAddressDTO> data = new PageUtils<>(pagrUtils);
        data.setList(list1);
        return Result.success(data);
    }
}
